package cn.actoncode.boot.framework.common.http;

import cn.actoncode.boot.framework.common.exception.ServiceErrorException;
import cn.actoncode.boot.framework.common.http.apache.ApacheMediaUploadRequestExecutor;
import cn.actoncode.boot.framework.common.http.enums.PlatformType;
import cn.actoncode.boot.framework.common.http.okhttp.OkHttpMediaUploadRequestExecutor;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * 上传媒体文件请求执行器.
 * 请求的参数是File, 返回的结果是String
 *
 * @author Daniel Qian
 */
public abstract class MediaUploadRequestExecutor<H, P> implements RequestExecutor<String, File> {
    protected RequestHttp<H, P> requestHttp;

    public MediaUploadRequestExecutor(RequestHttp requestHttp) {
        this.requestHttp = requestHttp;
    }

    @Override
    public void execute(String uri, Map<String, String> headers, File data, ResponseHandler<String> handler, PlatformType platformType) throws ServiceErrorException, IOException {
        handler.handle(this.execute(uri, headers, data, platformType));
    }

    public static RequestExecutor<String, File> create(RequestHttp requestHttp) {
        switch (requestHttp.getRequestType()) {
            case APACHE_HTTP:
                return new ApacheMediaUploadRequestExecutor(requestHttp);
            case OK_HTTP:
                return new OkHttpMediaUploadRequestExecutor(requestHttp);
            default:
                return null;
        }
    }

}
